package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import services.FacadeVistaWeb;
import services.ServiceLocator;
import entities.VO.RodamientoStockVO;

/**
 * Servlet implementation class AltaProveedorServlet
 */
public class AltaRodamientoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String codigo = request.getParameter("codigo");
		String caracteristicas = request.getParameter("caracteristicas");
		String marca = request.getParameter("marca");
		String origen = request.getParameter("origen");
		
		Integer id = null;
		if (request.getParameter("id")!=null && !"".equals(request.getParameter("id"))){
			id = Integer.parseInt(request.getParameter("id")); 
		}
		Integer stock = null;
		if (request.getParameter("stock")!=null && !"".equals(request.getParameter("stock"))){
			stock = Integer.parseInt(request.getParameter("stock")); 
		}
		
		RodamientoStockVO rodamientoStockVO = new RodamientoStockVO(id,caracteristicas,codigo,marca,origen,stock);
		
		ServiceLocator serviceLocator = new ServiceLocator(); 
		FacadeVistaWeb facade = serviceLocator.getFacade();
		if (id == null){
			facade.altaRodamiento(rodamientoStockVO);
		}else{
			rodamientoStockVO.setId(id);
			facade.actualizarRodamiento(rodamientoStockVO);
		}
		
		request.getRequestDispatcher("listarodamiento").forward(request, response);
	}

}
